package com.agung.wonogirimaps;

import java.util.List;

import org.json.JSONObject;

import com.agung.wonogirimaps.adapter.HotelAdapter;
import com.agung.wonogirimaps.entity.Hotel;
import com.agung.wonogirimaps.util.JSONAdapter;
import com.agung.wonogirimaps.util.Utils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class HotelActivity extends ActionBarActivity implements
		OnItemClickListener
{
	public static final String	TAG_NAMA			= "nama";
	public static final String	TAG_FASILITAS		= "fasilitas";
	public static final String	TAG_NOMOR_TELEPON	= "nomor";
	public static final String	TAG_ALAMAT			= "alamat";
	public static final String	TAG_KELAS			= "kelas";
	public static final String	TAG_GAMBAR			= "gambar";
	public static final String	TAG_LAT				= "lat";
	public static final String	TAG_LNG				= "lng";

	private ListView			lv;
	private JSONAdapter			json;
	private HotelAdapter		adapter;
	private ProgressDialog		pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hotel);
		lv = (ListView) findViewById(R.id.list_hotel);
		lv.setOnItemClickListener(this);

		getSupportActionBar().setTitle("Daftar Hotel");
		getSupportActionBar().setIcon(android.R.color.transparent);

		json = new JSONAdapter();

		new AsyncHotel().execute(Utils.API_HOTEL);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.hotel, menu);
		return true;
	}

	private class AsyncHotel extends AsyncTask<String, Void, List<Hotel>>
	{
		@Override
		protected List<Hotel> doInBackground(String... params)
		{
			JSONObject obj = json.getJSONFromURL(params[0]);

			return json.getAllHotel(obj);
		}

		@Override
		protected void onPostExecute(List<Hotel> result)
		{
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			pDialog.dismiss();
			adapter = new HotelAdapter(HotelActivity.this, result);
			lv.setAdapter(adapter);
		}

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();

			pDialog = new ProgressDialog(HotelActivity.this);
			pDialog.setMessage("Loading");
			pDialog.setCancelable(true);
			pDialog.show();
		}
	}

	@Override
	public void onBackPressed()
	{
		// TODO Auto-generated method stub
		super.onBackPressed();
		startActivity(new Intent(this, MainActivity.class));
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3)
	{
		Bundle bundle = new Bundle();
		bundle.putString(TAG_NAMA,
				((Hotel) adapter.getItem(position)).getNama());
		bundle.putString(TAG_FASILITAS,
				((Hotel) adapter.getItem(position)).getFasilitas());
		bundle.putString(TAG_NOMOR_TELEPON,
				((Hotel) adapter.getItem(position)).getTelepon());
		bundle.putString(TAG_KELAS,
				((Hotel) adapter.getItem(position)).getKelas());
		bundle.putString(TAG_ALAMAT,
				((Hotel) adapter.getItem(position)).getAlamat());
		bundle.putString(TAG_GAMBAR,
				((Hotel) adapter.getItem(position)).getGambar());
		bundle.putDouble(TAG_LAT, ((Hotel) adapter.getItem(position)).getLat());
		bundle.putDouble(TAG_LNG, ((Hotel) adapter.getItem(position)).getLng());

		Intent i = new Intent(this, InfoHotelActivity.class);
		i.putExtras(bundle);
		startActivity(i);
	}
}
